package com.gsit.misn.utils;

import java.io.FileOutputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import com.gsit.misn.domain.Patient;
import com.gsit.misn.domain.User;

import static  com.gsit.misn.utils.Constant.BLANK;

public class ExcelWriter implements Writer<User, List<Patient>> {

	private FileOutputStream fileOut;
	private HSSFWorkbook workbook = new HSSFWorkbook();

	@Override
	public void write(User user, List<Patient> patients) {
		// Date date = new Date();
		// filaName =
		// "Patient_"+userName+"_"+date.getDate()+"_"+date.getMonth()+"_"+(date.getYear()+1900)+".xls";
		try {
			this.fileOut = new FileOutputStream(Path.xlsPath + user.getXlsName());
			this.createFile(patients);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void createFile(List<Patient> patients) throws Exception {

		HSSFSheet worksheet = workbook.createSheet("Patient Detail");

		this.createHeader(worksheet);

		for (int rowNo = 0; rowNo < patients.size(); rowNo++) {
			if(patients.get(rowNo)!=null){
				this.createBody(worksheet, patients.get(rowNo), rowNo + 1);
			}
		}

		workbook.write(fileOut);
		fileOut.flush();
		fileOut.close();

	}
	
private void createHeader(HSSFSheet worksheet){
		
		HSSFRow header = worksheet.createRow((short) 0);
		
		int i=0;
		
		createHeaderHSSFCell(header , "ID", i++);
		createHeaderHSSFCell(header , "Patient ID", i++);
		createHeaderHSSFCell(header , "Patient Name", i++);
		createHeaderHSSFCell(header , "Doctor Name", i++);
		createHeaderHSSFCell(header , "Relative Name", i++);
		createHeaderHSSFCell(header , "Adderss", i++);
		createHeaderHSSFCell(header , "City", i++);
		createHeaderHSSFCell(header , "State",i++ );
		createHeaderHSSFCell(header , "Zip", i++);
		createHeaderHSSFCell(header , "Country", i++);
		createHeaderHSSFCell(header , "Phone", i++);
		createHeaderHSSFCell(header , "Email", i++);
		createHeaderHSSFCell(header , "Gender", i++);
		createHeaderHSSFCell(header , "Date of Birth", i++);
		createHeaderHSSFCell(header , "Height", i++);
		createHeaderHSSFCell(header , "Weight", i++);
		createHeaderHSSFCell(header , "Occupation", i++);
		createHeaderHSSFCell(header , "Marital Status", i++);
		createHeaderHSSFCell(header , "Edit Url", i++);
		createHeaderHSSFCell(header , "Search Url", i++);
		createHeaderHSSFCell(header , "Row No", i++);
		
	}
	
	private void createBody(HSSFSheet worksheet,Patient patient ,Integer rowNo){
		
		
		
		HSSFRow header = worksheet.createRow( rowNo);
		int i=0;
		createBodyHSSFCell(header , patient.getId(), i++);
		createBodyHSSFCell(header , patient.getPatientID(), i++);
		createBodyHSSFCell(header , patient.getPatientName(), i++);
		createBodyHSSFCell(header , patient.getDoctorName(), i++);
		createBodyHSSFCell(header , patient.getRelativeName(), i++);
		createBodyHSSFCell(header , patient.getAddress(), i++);
		createBodyHSSFCell(header , patient.getCity(), i++);
		createBodyHSSFCell(header , patient.getState(), i++);
		createBodyHSSFCell(header , patient.getZip(), i++);
		createBodyHSSFCell(header , patient.getCountry(), i++);
		createBodyHSSFCell(header , patient.getPhone(), i++);
		createBodyHSSFCell(header , patient.getEmail(), i++);
		createBodyHSSFCell(header , patient.getGender(), i++);
		createBodyHSSFCell(header , patient.getDateofBirth(), i++);
		createBodyHSSFCell(header , patient.getHeight(), i++);
		createBodyHSSFCell(header , patient.getWeight(), i++);
		createBodyHSSFCell(header , patient.getOccupation(), i++);
		createBodyHSSFCell(header , patient.getMaritalStatus(), i++);
		createBodyHSSFCell(header , patient.getEditURL(), i++);
		createBodyHSSFCell(header , patient.getSearchURL(), i++);
		createBodyHSSFCell(header , patient.getRowNo().toString(), i++);
		
		

		
	}
	
	private void createHeaderHSSFCell(HSSFRow row , String name, int pos){
		HSSFCell cell = row.createCell((short) pos);
		cell.setCellValue(name);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle = workbook.createCellStyle();
		cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
		cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		cell.setCellStyle(cellStyle);
		
		
	}
	
	
	private void createBodyHSSFCell(HSSFRow row , String value, int pos){
		HSSFCell cell = row.createCell(pos);
		if(StringUtils.isNotBlank(value)){
			cell.setCellValue(value);	
		}else{
			cell.setCellValue(BLANK);
		}
			
		
		
	}

}
